hfrApp.registerCtrl("MonitorPostedController", function ($scope, $modal, $http, $log, $window, $translate, $rootScope, $timeout, HfrConfig, HfrStorage, HfrInit) {
	//Init message alert
	HfrInit.message();
	
	//Display meta header
	HfrConfig.displayMeta("monitorPosted");
	
	//Delay content for display after 0,8 second
	$timeout(function () {
		$scope.showViewContent = true;
	}, 800);
	
	//Init startTime and endTime is datepicker
	$scope.startTime = new Date();
	$scope.startTime.setHours(0,0,0,0);
	
	$scope.endTime = new Date();
	$scope.endTime.setHours(23,59,59,999);
	
	$scope.format = 'dd-MM-yyyy';
	
	//Init paginator page
	$scope.showNext = false;
	$scope.showPrev = false;
	$scope.page = 1;
	$scope.limit = 12;
	
	$scope.showSocial = [];
	
	//Check is search data
	$scope.searching = false;
	
	$scope.openStartTime = function($event) {
	    $event.preventDefault();
	    $event.stopPropagation();

	    $scope.openedStartTime = true;
	};
	
	$scope.openEndTime = function($event) {
	    $event.preventDefault();
	    $event.stopPropagation();

	    $scope.openedEndTime = true;
	};
	
	//Set default style view
	$scope.gridStyle = true;
	
	$scope.changeViewList = function () {
		$scope.gridStyle = false;
	};
	
	$scope.changeViewGrid = function () {
		$scope.gridStyle = true;
	};
	
	//Get list blog
	var blogUrl = HfrConfig.ApiServiceUrl.blogsUrlApi + "?customerId=" + $rootScope.username;
	$scope.blogs = [];
	$http.get(blogUrl)
		.success(function (data) {
			if (data.status == 200) {
				angular.forEach(data.response.blogs, function(blog, key) {
					$scope.blogs.push(blog);
				});
			} else {
				$rootScope.reload.display = true;
				$rootScope.reload.text = data.message;
			}
		})
		.error(function (data) {
			$rootScope.reload.display = true;
			$rootScope.reload.text = $translate.instant("message.error.connect");
		});
	
	
	//Get all postsuccess for customer
	$scope.getPostedSuccess = function () {
		var postedCustomerUrl = HfrConfig.ApiServiceUrl.postSuccessUrlApi + "?customerId=" + $rootScope.username; //+ "&limit=" + $scope.limit + "&page=" + $scope.page;
		$http.get(postedCustomerUrl)
			.success(function (data) {
				$scope.showSuccessPosted = true;
				
				if (data.status == 200) {
					$scope.listPostSuccess = data.response.postSuccess;
					$scope.total = $scope.sizeOf(data.response.postSuccess);
					
				} else {
					$scope.total = 0;
					$rootScope.reload.display = true;
					$rootScope.reload.text = data.message;
				}
			})
			.error(function (data) {
				$scope.total = 0;
				$scope.showSuccessPosted = true;
				$rootScope.reload.display = true;
				$rootScope.reload.text = $translate.instant("message.error.connect");
			});
	};
	
	$scope.getPostedSuccess();
	
	
	
	//Search data
	$scope.search = function (form, reset) {
		//check form valid
		$scope.submitted = true;
		if (angular.isDate($scope.startTime) == false) {
			$scope.startTime = new Date($scope.startTime);
		}
		if (angular.isDate($scope.endTime) == false) {
			$scope.endTime = new Date($scope.endTime);
		}
		
		$scope.showSuccessPosted = false;
		$scope.searching = true;
		if (reset) {
			$scope.page = 1;
		}
		
		var clientTimezone = new Date().getTimezoneOffset(); //unit minute
		
		$scope.startTime = $scope.startTime.setHours(0,0,0,0);
		$scope.endTime = $scope.endTime.setHours(23,59,59,999);
		
		var startTime = new Date($scope.startTime).getTime();
		var endTime = new Date($scope.endTime).getTime();
		
		//Convert to timezon server
		startTime = startTime + (clientTimezone*60*1000);
		endTime = endTime + (clientTimezone*60*1000);
		
		var blogId = "";
	
		if ($scope.blogSelected !== undefined) {
			blogId = encodeURI($scope.blogSelected.url);
		}
		
		$scope.total = 0;
		
		$scope.showReload = {};
		//Get data
		var postedUrl = HfrConfig.ApiServiceUrl.postSuccessUrlApi + "?blogId=" + blogId + "&startTime=" + startTime + "&endTime=" + endTime + "&limit=" + $scope.limit + "&page=" + $scope.page;
		console.log(postedUrl);
		$http.get(postedUrl)
			.success(function (data) {
				
				if (data.status == 200) {
					$scope.showSuccessPosted = true;
					$scope.listPostSuccess = data.response.postSuccess;
					$scope.total = $scope.sizeOf(data.response.postSuccess);
					if ($scope.total == $scope.limit) {
						$scope.showNext = true;
					} else {
						$scope.showNext = false;
					}
					
					if ($scope.page > 1) {
						$scope.showPrev = true;
					} else {
						$scope.showPrev = false;
					}
				} else {
					$scope.total = 0;
					$scope.showReload = {
						display	: true,
						text	: data.message
					};
				}
			})
			.error(function (data) {
				$scope.total = 0;
				$scope.showReload = {
					display	: true,
					text	: $translate.instant("message.error.connect")
				};
			});
	};
	
	//demo
	$scope.shareSocial = function(url, width, height) {
		
		var left = Math.round(screen.width / 2 - width / 2);
	    var top = 0;
		if (screen.height > height) {
		    top = Math.round(screen.height / 3 - height / 2);
		}
		
		var win = $window.open(
		    url,
		    'left=' + left + ',top=' + top + ',' +
		    'width=' + width + ',height=' + height +
		    ',personalbar=0,toolbar=0,scrollbars=1,resizable=1'
		);
		
		if (win) {
		    win.focus();
		} else {
		    $location.href = url;
		}
		
		return false;
	};
	
	//Count size
	$scope.sizeOf = function(obj) {
	    return Object.keys(obj).length;
	};
	
	//Change display share
	$scope.displayShare = function(index) {
		if ($scope.showSocial[index] !== true) {
			$scope.showSocial[index] = true;
		} else {
			$scope.showSocial[index] = false;
		}
	};
	
	//Share social
	$scope.shareFacebook = function (postSuccess) {
		var shareUrl = $scope.getUrl(postSuccess);
		var url = "http://www.facebook.com/share.php?u="+encodeURI(shareUrl)+"&title="+encodeURI(postSuccess.title);
		$scope.shareSocial(url, 600, 500);
	};
	
	$scope.shareGoogle = function (postSuccess) {
		var shareUrl = $scope.getUrl(postSuccess);
		var url = "https://plus.google.com/share?url="+encodeURI(shareUrl);
		$scope.shareSocial(url, 700, 500);
	};
	
	$scope.shareTwitter = function (postSuccess) {
		var shareUrl = $scope.getUrl(postSuccess);
		var url = "http://twitter.com/home?status="+encodeURI(postSuccess.title)+"+"+encodeURI(shareUrl);
		$scope.shareSocial(url, 600, 450);
	};
	
	$scope.shareLinkedIn = function (postSuccess) {
		var shareUrl = $scope.getUrl(postSuccess);
		var url = "ttp://www.linkedin.com/shareArticle?mini=true&url="+encodeURI(shareUrl)+"&title="+encodeURI(postSuccess.title);
		$scope.shareSocial(url, 600, 500);
	};
	
	//Get url post
	$scope.getUrl = function (postSuccess) {
		var shareUrl = "";
		if (postSuccess.blog.type == "Wordpress") {
			shareUrl = postSuccess.blogId + "/?p=" + postSuccess.link;
		} else {
			shareUrl = postSuccess.link;
		}
		
		return shareUrl;
	};
	
	$scope.prevPage = function (form) {
		if ($scope.showPrev) {
			$scope.page = $scope.page - 1;
			if ($scope.searching) {
				$scope.search(form, false);
			} else {
				$scope.getPostedSuccess();
			}
		}
	};
	
	$scope.nextPage = function (form) {
		if ($scope.showNext) {
			$scope.page = $scope.page + 1;
			if ($scope.searching) {
				$scope.search(form, false);
			} else {
				$scope.getPostedSuccess();
			}
		}
	};
});

